define(function (require) {
    require('table');

    $.module("BASE.Material", function () {

        return {
            init: function () {
                this.loadData();
                this.loadEvent();
            },
            clearDatas:function () {
                var that = this;

                $("#_id").val(0);

                $('#fm').bootstrapValidator('resetForm', true);
                that.showDiv('main_div');
            },
            loadData: function (pageNumber, pageSize) {
                var that = this;

                let s_Id = $("#s_Id").val();
                let s_Code = $("#s_Code").val();
                let s_Name = $("#s_Name").val();

                SYS.Core.ajax({
                    url: "base/material/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        Id: s_Id,
                        Code: s_Code,
                        Name: s_Name
                    },
                    success: function (data) {
                        let obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };

                        $("#myTable").bootstrapTable('destroy');
                        $('#myTable').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                // {field: 'checked', checkbox: true} ,
                                {field: 'Id', title: '产品ID', align: 'center'},
                                {field: 'Code', title: '产品编码', align: 'center'},
                                {field: 'Name', title: '产品名称', align: 'center'},
                                {field: 'GroupName', title: '产分类别', align: 'center'},
                                {field: 'SeriesName', title: '产品系列', align: 'center'},
                                {field: 'NatureName', title: '产品性质', align: 'center'},
                                {field: 'PackageName', title: '包装', align: 'center'},
                                {field: 'CapacityName', title: '容量', align: 'center'},
                                {field: 'QtyPerPack', title: '每箱瓶数', align: 'center'},
                                {field: 'QtyPerStdPack', title: '标准箱瓶数', align: 'center'},
                                {field: 'BaseUnit', title: '计量单位', align: 'center'},
                                // {
                                //     field: 'do', title: '操作', align: 'center',
                                //     formatter: function (value, row, index) {
                                //         var html = '';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.Material.toEdit(' + row.id + ')" class="btn btn-edit">编辑</a> | ';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.Material.toRemove(' + row.id + ')" class="btn btn-remove  ">删除</a>';
                                //         return html;
                                //     }
                                // }
                            ],
                            onPageChange: function(number, size){
                                that.loadData(number, size);
                            },
                            onRefreshTable: function () {
                                that.loadData();
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    that.loadData();
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        that.loadData();
                    }
                });

                // 返回
                $(".back_list").on('click', function () {
                    that.clearDatas();
                    that.showDiv('main_div');
                });

            },
            toEdit: function (id) {
                var that = this;

                that.clearDatas();
                $("#btn-add-div").hide();
                $("#btn-edit-div").show();

                SYS.Core.ajax({
                    url: 'base/material/detail',
                    common: {},
                    data: {
                        FID: id ? id : 0
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            let obj = data.data;

                            that.showDiv('edit_div');
                        }
                    }
                });
            },
            showDiv: function (div) {
                var divs = ['main_div','edit_div','import_div'];
                for(var i=0; i<divs.length; i++){
                    $("#" + divs[i]).hide();
                }
                $("#" + div).show();
            }
        }
    });
    BASE.Material.init();
});